//
//  functionVC.swift
//  swiftDemo
//
//  Created by mac on 2017/3/14.
//  Copyright © 2017年 毕小强. All rights reserved.
//

/**
 *
 * ......................我佛慈悲......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG...................
 *
 *          函数,干巴爹
 *
 */

import UIKit

class functionVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
        print(greet(person: "Jack"))
        let des = greet1(name: "Lucy", age: "28")
        print("\(des.nameDes) \(des.ageDes)")
        if let bounds = minMax(arr: [1,2,3,4,5,6,7]) {
            print("max is \(bounds.max),min is \(bounds.min)")
        }
        print(noFunction("Lucy", age: 21))
        defalutParametersFunction(defalut:1)
        defalutParametersFunction()
        print(average(1,2,3,4,5))
        var name = "李磊"
        var age = 27
        varFunction(name: &name, age: &age)
        print("都会变成\(name)\(age)岁了")
        let newVarFunction = varFunction
        newVarFunction(&name, &age)
        print(funcInFunc(average, one: 10, two: 20))
        
        var step = 3
        let moveZero = moveToZero(step > 0)
        while step != 0 {
            print(step)
            step =  moveZero(step)
        }
        print("zero!!!")

    }
    
    /// 一个形参 有返回值的函数
    ///
    /// - Parameter person: 第一个形参
    /// - Returns: 返回值
    func greet(person:String) -> String {
        return "Hello" + person + "!"
    }
    
    /// 多个形参
    ///
    /// - Parameters:
    ///   - name: 第一个形参
    ///   - age: 第二个形参
    /// - Returns: 多个返回值以元组形势返回
    func greet1(name:String,age:String) -> (nameDes:String,ageDes:String) {
        return ("Hello"+name+"!","you are"+age)
    }
    
    
    /// 返回一个可选值函数
    ///
    /// - Parameter arr: 形参
    /// - Returns: 返回一个可选的值
    func minMax(arr:[Int]) -> (min:Int,max:Int)? {
        if arr.isEmpty {return nil}
        var minValue = arr[0]
        var maxValue = arr[0]
        for value in arr {
            if value<minValue {
                minValue = value
            }else if value>maxValue{
                maxValue = value
            }
        }
        return (minValue,maxValue)
    }
    
    /// 在提供形式参数名之前写实际参数标签，用空格分隔
    /// 形势参数名name用来调用函数显示 实际参数名age用来函数内调用
    /// - Parameter age: 年龄
    func someFunction(name age:Int)  {
        print(age)
    }
    
    
    /// 省略实际参数标签
    ///
    /// - Parameters:
    ///   - name: 第一个参数
    ///   - age: 第二个参数
    /// - Returns: 返回值
    func noFunction(_ name:String,age:Int) -> String {
        return name+"is"+String(age)
    }
    
    /// 默认形式参数
    ///
    /// - Parameter defalut: 可传值 可不传值
    func defalutParametersFunction(defalut:Int = 9)  {
        print(defalut)
    }
    
    /// 可变形式参数
    ///
    /// - Parameter number: 若干参数
    /// - Returns: 平均值
    func average(_ number:Int...) -> Double {
        var sum  = 0
        for a in number {
            sum += a
        }
        return Double(sum)/Double(number.count)
    }
    
    /// 输入输出形式参数 允许通过函数修改传入的变量
    ///
    /// - Parameters:
    ///   - name: 可修改变量1
    ///   - age: 可修改变量2
    func varFunction(name:inout String,age:inout Int)  {
        name = "韩梅梅"
        age = 12
    }
    
    /// 函数类型作为形参
    ///
    /// - Parameters:
    ///   - mathFunction: 函数形参
    ///   - one: 传入的第一个值
    ///   - two: 传入的第二个值
    /// - Returns: 返回值
    func funcInFunc(_ mathFunction:(Int...)->Double,one:Int,two:Int) -> Double {
        return mathFunction(one, two)
    }
    
    
    /// 返回值为函数类型&&内嵌函数
    ///
    /// - Parameter currentvalue: 参数
    /// - Returns: 函数类型的返回值
    func moveToZero(_ currentvalue:Bool) -> (Int) -> Int{
        func stepForward(_ input:Int) ->Int {
            return input + 1
        }
        func stepBackward(_ input:Int) ->Int {
            return input - 1
        }
        return currentvalue ? stepBackward : stepForward
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
